package com.weitu.workflow.flowBusiness.controller;

import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.IDGenerator;
import com.weitu.workflow.flowBusiness.entity.FlowBusiness;
import com.weitu.workflow.flowBusiness.service.FlowBusinessService;
import com.weitu.workflow.flowManagement.entity.WorkItemInfo;
import com.weitu.workflow.flowManagement.service.FlowManagementService;
import org.fireflow.engine.IProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/****
 * 流程业务标识管理Controller
 */
@Controller
@RequestMapping("/workflow/flowBusiness")
public class FlowBusinessController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "workflow/flowBusiness";
    @Autowired
    private FlowBusinessService flowBusinessService;//流程业务标识管理Service

    /***
     * 跳转到业务标识列表页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public ModelAndView todoList(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/list");
        return modelAndView;
    }
    /***
     * 查询流程标识列表数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/findFlowBusinessList")
    public Object findFlowBusinessList(HttpServletRequest request) {

        User currentUser=this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        this.putParametersIntoMap(map);
        map.put("userName",currentUser.getName());
        Integer totalNum = this.flowBusinessService.queryFlowBusinessCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView",pageView);
        List<FlowBusiness> flowBusinessesList =this.flowBusinessService.queryFlowBusinessList(map);

        return pagination.flushDataGridData(flowBusinessesList);
    }
    /***
     * 跳转到业务标识添加页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }
    /***
     * 跳转到业务标识编辑页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String businessIdentity = request.getParameter("businessIdentity");
        //查询流程业务信息
        FlowBusiness flowBusiness = flowBusinessService.queryFlowBusinessByIdentity(businessIdentity);
        modelAndView.addObject("flowBusiness",flowBusiness);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        return modelAndView;
    }
    /***
     * 验证业务标识唯一性
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkBusinessIdentity")
    public AjaxResult checkBusinessIdentity(HttpServletRequest request) {

        User currentUser=this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        this.putParametersIntoMap(map);


        putParametersIntoMap(map);

        String identity=map.get("identity").toString();//业务标识
        FlowBusiness flowBusiness=this.flowBusinessService.queryFlowBusinessByIdentity(identity);

        AjaxResult result = new AjaxResult();
        if (flowBusiness==null){
            result.setMessage("业务标识不存在！");
            result.setSuccess(true);
        }else {
            result.setMessage("业务标识已存在！");
            result.setSuccess(false);
        }
        return result;
    }
    /***
     * 添加流程业务信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addFlowBusiness")
    public AjaxResult addFlowBusiness(HttpServletRequest request) {

        User currentUser=this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        this.putParametersIntoMap(map);


        putParametersIntoMap(map);

        String id= IDGenerator.getId();
        map.put("id",id);
        boolean boo=this.flowBusinessService.addFlowBusiness(map);

        AjaxResult result = new AjaxResult();
        if (boo){
            result.setMessage("保存成功！");
            result.setSuccess(true);
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
        }
        return result;
    }
    /***
     * 添加流程业务信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateFlowBusiness")
    public AjaxResult updateFlowBusiness(HttpServletRequest request) {

        User currentUser=this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        this.putParametersIntoMap(map);


        putParametersIntoMap(map);

        boolean boo=this.flowBusinessService.updateFlowBusiness(map);

        AjaxResult result = new AjaxResult();
        if (boo){
            result.setMessage("保存成功！");
            result.setSuccess(true);
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
        }
        return result;
    }
    /***
     * 添加流程业务信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteFlowBusiness")
    public AjaxResult deleteFlowBusiness(HttpServletRequest request) {

        User currentUser=this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        this.putParametersIntoMap(map);


        putParametersIntoMap(map);

        boolean boo=this.flowBusinessService.deleteFlowBusiness(map);

        AjaxResult result = new AjaxResult();
        if (boo){
            result.setMessage("删除成功！");
            result.setSuccess(true);
        }else {
            result.setMessage("删除失败！");
            result.setSuccess(false);
        }
        return result;
    }
    /***
     * 验证业务是否可删除
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkBusinessCanDelete")
    public AjaxResult checkBusinessCanDelete(HttpServletRequest request) {

        User currentUser=this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        this.putParametersIntoMap(map);


        putParametersIntoMap(map);

        //String identity=map.get("identity").toString();//业务标识
        //根据查询出来的数量判断是否已经加入工作流
        boolean boo=this.flowBusinessService.queryFlowBusinessIsInWorkFlow(map);


        AjaxResult result = new AjaxResult();
        if (boo){
            result.setMessage("业务标识没有加入工作流！");
            result.setSuccess(true);
        }else {
            result.setMessage("业务标识已经加入工作流！");
            result.setSuccess(false);
        }
        return result;
    }


}
